var orderby = "order by id";
var pageSize = 10;
$(document).ready(function() {
	initData(0);
});
// 翻页响应
function pageselectCallback(page_id) {
	initData(page_id);
}

function initData(pageindx) {
	var tbody = "";
	var pageCount = "";
	$
			.ajax({
				contentType : "application/x-www-form-urlencoded; charset=utf-8",
				type : "POST",// 用POST方式传输
				dataType : "json",// 数据格式:JSON
				url : 'find.action',// 目标地址
				async : false,// 作用是防止在ajax成功调用之前就调用$("#Pagination").pagination,这个时候数据个数还没有初始化
				data : "page.page=" + (pageindx + 1) + "&tableData.orderby="
						+ escape(orderby) + "&page.PageSize=" + pageSize,
				beforeSend : function() {
					$("#divload").show();
					$("#Pagination").hide();
				},// 发送数据之前
				complete : function() {
					$("#divload").hide();
					$("#Pagination").show();
				},// 接收数据完毕
				error : function() {
					alert("error");
				},
				success : function(json) {
					$("#productTable tr:gt(0)").remove();
					pageCount = json[0];
				    if (json == "") {
						$("#divload").hide();
						dis();
						return false;
					}

					$
							.each(
									json,
									function(i, n) {
										if (i>0) {
											var trs = "";
											trs += "<tr><td align='center'>"
													+ n[0]
													+ "</td><td align='center'>"
													+ n[1]
													+ "</td><td align='center'>"
													+ n[2]
													+ "</td><td align='center'>"
													+ n[3]
													+ "</td><td align='center'>";//<a href=\"javascript:edit('')\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:view('')\">查看</a></td></tr>
											tbody += trs;
										}
									});
					$("#productTable").append(tbody);
					// 行交替颜色
					$("#productTable tr:gt(0):odd").attr("class", "red");
					$("#productTable tr:gt(0):even").attr("class", "blue");
				}// end of success

			});
	if (pageCount != 0) {
		$("#Pagination").pagination(pageCount, {
			callback : pageselectCallback,
			prev_text : '<< 上一页',
			next_text : '下一页 >>',
			items_per_page : pageSize,
			num_display_entries : 10,
			current_page : pageindx,
			num_edge_entries : 2,
			link_to : "pageindex.jsp#"

		});
	}
}
// 排序
function Sort(ordercolumn, ordertipid) {
	var ordertype = "";// 1:desc,0:asc
	var $orderimg = $("#" + ordertipid);
	if ($orderimg.html() != "") {
		var imgsrc = "";
		imgsrc = $("img", $orderimg).attr("src");
		if (imgsrc.indexOf("asc") > -1) {
			$(".ordertip").empty();
			$orderimg
					.html("&nbsp;<img src=\"images/sort_desc.gif\" align=\"absmiddle\">");
			ordertype = 1;

		} else {
			$(".ordertip").empty();
			$orderimg
					.html("&nbsp;<img src=\"images/sort_asc.gif\" align=\"absmiddle\">");
			ordertype = 0;
		}
	} else {
		$(".ordertip").empty();
		$orderimg
				.html("&nbsp;<img src=\"images/sort_desc.gif\" align=\"absmiddle\">");
		ordertype = 1;
	}
	if (ordertype == 0) {
		orderby = "order by " + ordercolumn + " asc ";
	} else {
		orderby = "order by " + ordercolumn + " desc ";
	}
	initData(0);

}